import store from '@/store'
import { findStrNindex } from './index'

/**
 * @Author Tangyuhui
 * @Description 是否拥有访问权限
 * @param {要访问的地址} path
 * @return {是否拥有权限} flag
 * @Date 2019-11-26 14:30:30 Tuesday
 */
export function isHaveMenuPermission(path) {
  const permissionMenu = store.state.bms.permissionMenu
  const index = findStrNindex(path, '/', 2)
  if (index !== -1) {
    path = path.substring(0, index)
  }
  let flag = false
  check(permissionMenu)
  function check(menuList) {
    if (!flag) {
      if (Array.isArray(menuList)) {
        for (const menu of menuList) {
          if (menu.menus) {
            check(menu.menus)
          } else {
            if (menu.item && menu.item.route === path) {
              flag = true
            }
          }
        }
      }
    }
  }
  return flag
}

/**
 * @Author tangyuhui
 * @Description 根据code判断是否要显示按钮（按钮权限控制）
 * @Date 2021-03-21 11:21:11 Sunday
 */
export function isHaveButtonPermission(code) {
  const buttonList = store.state.bms.permissionButton
  let flag = false
  if (Array.isArray(buttonList)) {
    const index = buttonList.findIndex((button) => {
      return button.moduleCode === code
    })
    if (index !== -1) {
      /** 找到了 */
      flag = true
    }
  }
  return flag
}

